import time 
import random                    
timestamp = str(int(time.time() )) 

def get_next_sequence(self,preset: str, batch_size: int = 1) -> List[str]:
    """生成产品编号的优化函数
    
    Args:
        preset: 7位预设字符串（格式：D1YYMMGM）
        batch_size: 需要生成的编号数量
        
    Returns:
        按顺序生成的产品编号列表
    """
    if len(preset) != 7:
        raise ValueError("Invalid preset format. Must be 7 characters.")
    
    current_year = datetime.now().strftime('%y')
    current_month = datetime.now().month
    
    with _lock:  # 保证线程安全
        cursor = _conn.cursor()
        
        # 获取或初始化序列
        cursor.execute('''INSERT OR IGNORE INTO product_sequence 
                        (preset, sequence, last_month) 
                        VALUES (?, 0, ?)''', (preset, current_month))
        
        # 检查月份变化
        cursor.execute('''UPDATE product_sequence
                        SET sequence = CASE WHEN last_month != ? THEN 0 ELSE sequence END,
                            last_month = ?
                        WHERE preset = ? 
                        RETURNING sequence''',
                     (current_month, current_month, preset))
        
        result = cursor.fetchone()
        start_seq = result[0] + 1 if result else 1
        
        # 批量更新序列号
        cursor.execute('''UPDATE product_sequence
                        SET sequence = sequence + ?
                        WHERE preset = ?''',
                     (batch_size, preset))
        
        _conn.commit()
        
    # 生成批量编号
    return [f"{preset}{i:04d}" for i in range(start_seq, start_seq + batch_size)]